<template>
  <div class="posManage">
    <div class="header">
      <h1>职位管理</h1>
      <div class="header-right">
        <i class="el-icon-s-home"></i>
        <span class="font-left">首页</span>
        <i class="el-icon-arrow-right"></i>
        <span class="font-right">职位管理</span>
      </div>
    </div>
    <div class="content">
      <div class="bar">
        <div>
          <el-button
            type="success"
            icon="el-icon-plus"
            size="mini"
            @click="addPos"
          >
            新增
          </el-button>
          <el-button type="info" size="mini" @click="resetList">
            <i class="el-icon-refresh-left"></i>
            重置</el-button
          >
        </div>
        <div class="barRight">
          <el-input
            @keyup.enter.native="search"
            clearable
            @clear="search"
            v-model="searchContent"
            placeholder="查找名称"
            class="barInput"
            size="mini"
          ></el-input>
          <el-button
            slot="append"
            icon="el-icon-search"
            size="mini"
            @click="search"
          ></el-button>
        </div>
      </div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :default-sort="{ prop: 'level', order: 'ascending' }"
      >
        <el-table-column prop="id" label="ID" width="60">
          <!-- <template scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template> -->
        </el-table-column>
        <el-table-column
          prop="title"
          label="名称"
          width="630"
        ></el-table-column>
        <el-table-column
          prop="level"
          label="层级"
          width="322"
          sortable
        ></el-table-column>
        <el-table-column prop="operate" label="操作">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="changePos(scope.row)"
              >修改</el-button
            >
            <el-button size="mini" type="danger" @click="goDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        @getPagination="getPagination"
        :total="total"
        :pageSize="pageSize"
      ></Pagination>
    </div>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
export default {
  name: "",
  components: {
    Pagination,
  },
  mixins: [],
  props: {},
  data() {
    return {
      input: "",
      total: 0,
      currentPage: 1,
      pageSize: 10,
      //搜索内容
      searchContent: "",
      tableData: [],
    };
  },
  computed: {},
  watch: {},
  mounted() {},
  created() {
    this.getAllPosList(this.currentPage, this.pageSize);
  },
  methods: {
    resetList() {
      this.getAllPosList(this.currentPage, this.pageSize);
    },
    //获取点击的页码
    getPagination(page) {
      this.currentPage = page;
      this.getAllPosList(this.currentPage, this.pageSize);
    },
    //获取表格数据
    async getAllPosList(currentPage, pageSize) {
      let res = await this.$api.user.getPosList({ currentPage, pageSize });
      if (res.data.code === 200) {
        console.log(res.data.data.list);
        this.tableData = res.data.data.list;
        //页码
        this.total = res.data.data.total;
      } else {
        console.log("请求失败");
        this.tableData = [];
        this.pageSize = 0;
        this.total = 1;
      }
    },
    //搜索功能
    getList() {
      this.stashList = this.tableData;
    },
    async search() {
      let res = await this.$api.user.searchPos(
        this.currentPage,
        this.pageSize,
        this.searchContent.toString()
      );
      this.tableData = res.data.data.list;
      this.total = res.data.data.total;
    },
    //增加按钮
    addPos() {
      this.$router.push("/user/addPos");
    },
    //更改页面
    changePos(row) {
      this.$router.push({
        name: "changePos",
        params: {
          row,
        },
      });
    },
    //删除
    goDelete(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.deleteConfirm(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async deleteConfirm(id) {
      let res = await this.$api.user.deletePos(id);
      console.log(res);
      if (res.data.code === 200) {
        this.$message({
          type: "success",
          message: "删除成功!",
        });
        //更新视图-------------------------
        this.getAllPosList(this.currentPage, this.pageSize);
      } else {
        this.$message.error("删除失败");
      }
    },
  },
};
</script>
<style lang="less" scoped>
.header {
  position: relative;
  padding: 15px;
  display: flex;
  justify-content: space-between;
}
.header > h1 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
}
.header-right {
  font-size: 12px;
  display: flex;
  align-items: center;
}
.el-icon-arrow-right {
  padding: 0 5px;
  color: #ccc;
}
.el-icon-s-home {
  color: #444;
  display: inline-block;
  margin-right: 5px;
}
.font-left {
  color: #444;
}
.font-right {
  color: #777;
}

.content {
  background-color: #f5f5f5;
}
.bar {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  padding: 10px;
  border-top: 3px solid #d2d6de;
  border-bottom: 1px solid #f4f4f4;
}
.barRight {
  display: flex;
}
.el-button-success {
  background-color: red;
}
.el-table-column {
  color: #333333;
  height: 37px;
}
</style>
